package cn.tutu.shop.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.tutu.blockchain.common.OrderStatusEnum;
import cn.tutu.blockchain.common.Page;
import cn.tutu.blockchain.common.PayTypeEnum;
import cn.tutu.blockchain.exception.BusinessException;
import cn.tutu.blockchain.integral.entity.IntegralRecord;
import cn.tutu.blockchain.integral.service.IntegralRecordService;
import cn.tutu.blockchain.sysuser.dao.SysUserDao;
import cn.tutu.blockchain.sysuser.entity.SysUser;
import cn.tutu.blockchain.utils.ArithUtils;
import cn.tutu.shop.dao.ExpressDao;
import cn.tutu.shop.dao.OrderDao;
import cn.tutu.shop.dao.OrderItemDao;
import cn.tutu.shop.entity.Express;
import cn.tutu.shop.entity.Order;
import cn.tutu.shop.entity.OrderItem;
import cn.tutu.shop.service.OrderService;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private OrderItemDao orderItemDao;
	@Autowired
	private IntegralRecordService integralRecordService;
	@Autowired
	private ExpressDao expressDao;

	@Override
	public void create(String userId, List<OrderItem> items) {
		Double total = 0.0;
		for (OrderItem item : items) {
			if (item.getCount() <= 0) {
				throw new BusinessException("商品个数必须大于0!");
			}
			item.setTotalPrice(ArithUtils.mul(item.getCount(), item.getTotalPrice()));
			total = ArithUtils.add(total, item.getTotalPrice());
		}
		SysUser user = sysUserDao.findById(userId);
		if (null == user) {
			throw new BusinessException("用户信息不存在!");
		}
		if (user.getIntegral() < total) {
			throw new BusinessException("用户积分不够!");
		}

		// 写入订单信息
		Order order = new Order();
		order.setUserId(userId);
		order.setOrderNum(getOrderNum());
		order.setPayType(PayTypeEnum.PAYTYPE_JF.getCode());
		order.setStatus(OrderStatusEnum.DONE.getCode());
		order.setTotal(total);
		orderDao.insert(order);

		// 写入订单商品信息
		Map<String, Object> parMap = new HashMap<String, Object>();
		parMap.put("orderId", order.getId());
		parMap.put("items", items);
		orderItemDao.insertBatch(parMap);

		// 扣除积分
		user.setIntegral(ArithUtils.sub(user.getIntegral(), total));
		sysUserDao.update(user);

		// 写入积分使用记录
		IntegralRecord integralRecord = new IntegralRecord();
		integralRecord.setIntegral(total);
		integralRecord.setUserId(user.getId());
		integralRecord.setRemark("兑换商品,订单号：" + order.getOrderNum());
		integralRecordService.create(integralRecord);
	}

	private String getOrderNum() {
		synchronized (this) {
			return System.currentTimeMillis() + "";
		}
	}

	@Override
	public PageInfo<Order> getByUser(String userId, Page page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<Order> list = orderDao.findByUser(userId);
		PageInfo<Order> pageInfo = new PageInfo<Order>(list);
		return pageInfo;
	}

	@Override
	public void createOnly(OrderItem item, Express express) {

		if (item.getCount() <= 0) {
			throw new BusinessException("商品个数必须大于0!");
		}
		item.setTotalPrice(ArithUtils.mul(item.getCount(), item.getProductPrice()));
		Double total = item.getTotalPrice();

		SysUser user = sysUserDao.findById(item.getUserId());
		if (null == user) {
			throw new BusinessException("用户信息不存在!");
		}
		if (user.getIntegral() < total) {
			throw new BusinessException("用户积分不够!");
		}

		expressDao.insert(express);
		// 写入订单信息
		Order order = new Order();
		order.setExpressId(express.getId());
		order.setUserId(item.getUserId());
		order.setOrderNum(getOrderNum());
		order.setPayType(PayTypeEnum.PAYTYPE_JF.getCode());
		order.setStatus(OrderStatusEnum.DONE.getCode());
		order.setTotal(total);
		orderDao.insert(order);

		// 写入订单商品信息
		item.setOrderId(order.getId());
		orderItemDao.insert(item);

		// 扣除积分
		user.setIntegral(ArithUtils.sub(user.getIntegral(), total));
		sysUserDao.update(user);

		// 写入积分使用记录
		IntegralRecord integralRecord = new IntegralRecord();
		integralRecord.setIntegral(0 - total);
		integralRecord.setUserId(user.getId());
		integralRecord.setRemark("兑换商品,订单号：" + order.getOrderNum());
		integralRecordService.create(integralRecord);
	}

}
